package com.example.venue_reservation_service.config;

import cn.hutool.json.JSON;
import com.example.venue_reservation_service.converter.ListConverter;
import com.example.venue_reservation_service.mapper.SensitiveWordMapper;
import com.example.venue_reservation_service.service.SensitiveWordService;
import com.example.venue_reservation_service.utils.RedisUtil;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class CacheInitializer {

    @Resource
    private SensitiveWordMapper sensitiveWordMapper;

    @Resource
    private RedisUtil redisUtil;



    private final static String REDIS_KEY = "sensitive-word";

    @PostConstruct // 在Bean初始化后执行[5](@ref)
    public void init() {
        List<String> list = sensitiveWordMapper.selectWordsOrderedByPriority();
        try {
            redisUtil.set(REDIS_KEY, ListConverter.listToString(list));
            log.info("敏感词库已加载到Redis");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("服务异常，" + e.getMessage());
        }
    }
}